package com.sikadwafastdaisfondturbo.pretloan.utils.dialog

import android.content.Context
import android.graphics.Color
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.WindowManager
import com.sikadwafastdaisfondturbo.pretloan.R
import com.sikadwafastdaisfondturbo.pretloan.databinding.DialogLanguageBinding
import com.sikadwafastdaisfondturbo.pretloan.pro.ProDialog
import com.sikadwafastdaisfondturbo.pretloan.utils.config.Configs
import com.sikadwafastdaisfondturbo.pretloan.utils.event.EventType
import com.sikadwafastdaisfondturbo.pretloan.utils.helper.SikadwaKefuConfigHelper
import com.sikadwafastdaisfondturbo.pretloan.utils.http.HttpHelper

/**
 * Author : wwdeng
 * Date : 2023/8/1 15:44
 * <p>
 * 每天进步一点点
 * <p>
 * Desc : 语言切换dialog
 */
class LanguageDialog: ProDialog {
    private lateinit var binding: DialogLanguageBinding
    private var mCallback: IDialogCallback? = null
    //事件备注
    private var eventRemark = ""

    //选择的语言类型
    private var seleceLang = ""

    override fun initWindow() {
        val params = window?.attributes
        params?.width = WindowManager.LayoutParams.MATCH_PARENT
        params?.height = WindowManager.LayoutParams.WRAP_CONTENT
        window?.attributes = params
        window?.setWindowAnimations(R.style.dialog_slide_anim) //修改动画样式
        window?.setGravity(Gravity.BOTTOM)
    }

    constructor(context: Context?) : super(context) {}
    constructor(context: Context?, callback: IDialogCallback?) : super(context) {
        mCallback = callback
    }

    constructor(context: Context?, remark: String,callback: IDialogCallback?) : super(context) {
        mCallback = callback
        eventRemark = remark
    }

    /**
     * 设置点击回调
     */
    fun setOnClick(callback: IDialogCallback){
        this.mCallback = callback
    }

    override fun getRootView(): View {
        binding = DialogLanguageBinding.inflate(LayoutInflater.from(context))
        return binding.root
    }

    override fun initView() {
    }

    override fun initListener() {
        setOnClickListener(R.id.langEnLayout)
        setOnClickListener(R.id.langSwLayout)
        setOnClickListener(R.id.btnLangConfirm)
    }

    override fun initData() {
        var defLanguage = Configs.getSPString(Configs.SPKEY.SIKADWA_LANGUAGE_CHOOSE)
        if (defLanguage.isEmpty()){
            //未选择过语言，从客服配置中取
            defLanguage = SikadwaKefuConfigHelper.takeSikadwaDefConfigLang()
        }
//        var appCurtLanguage = ""
//        val locale = AppCompatDelegate.getApplicationLocales()[0]
//        locale?.let {
//            appCurtLanguage = locale.language
//        }
        seleceLang = defLanguage
        showSikadwaSelectedLangTypeView()
    }

    override fun onViewClick(v: View?) {
        when(v?.id){
            R.id.langEnLayout -> {
                seleceLang = "en"
                HttpHelper.addEvent(String.format(EventType.CLICK.CLICK_LANGUAGE_CHECK,seleceLang))
                showSikadwaSelectedLangTypeView()
            }
            R.id.langSwLayout -> {
                seleceLang = "sw"
                HttpHelper.addEvent(String.format(EventType.CLICK.CLICK_LANGUAGE_CHECK,seleceLang))
                showSikadwaSelectedLangTypeView()
            }
            R.id.btnLangConfirm -> {
                if (seleceLang.isNotEmpty()){
                    HttpHelper.addEvent(String.format(EventType.CLICK.CLICK_LANGUAGE_SELECTED,seleceLang),seleceLang)
                    Configs.saveSPString(Configs.SPKEY.SIKADWA_LANGUAGE_CHOOSE,seleceLang)
                    mCallback?.onSikadwaSelectedLangType(seleceLang)
                    dismiss()
                }

            }
        }
    }

    /**
     * 设置选择的语言类型
     */
    private fun showSikadwaSelectedLangTypeView(){
        binding.langEnLayout.setBackgroundResource(R.drawable.sikadwa_lang_normal_bg)
        binding.langEnText.setTextColor(Color.parseColor("#FF999999"))
        binding.langEnImageView.setImageResource(R.mipmap.img_lang_choose_normal_icon)

        binding.langSwLayout.setBackgroundResource(R.drawable.sikadwa_lang_normal_bg)
        binding.langSwText.setTextColor(Color.parseColor("#FF999999"))
        binding.langSwImageView.setImageResource(R.mipmap.img_lang_choose_normal_icon)

        when(seleceLang){
            "en" -> {
                binding.langEnLayout.setBackgroundResource(R.drawable.sikadwa_lang_selected_bg)
                binding.langEnText.setTextColor(Color.parseColor("#FF000000"))
                binding.langEnImageView.setImageResource(R.mipmap.img_lang_choose_selected_icon)
            }
            "sw" -> {
                binding.langSwLayout.setBackgroundResource(R.drawable.sikadwa_lang_selected_bg)
                binding.langSwText.setTextColor(Color.parseColor("#FF000000"))
                binding.langSwImageView.setImageResource(R.mipmap.img_lang_choose_selected_icon)
            }
        }
    }

    interface IDialogCallback {
        fun onSikadwaSelectedLangType(lang: String)
    }

    override fun onStart() {
        super.onStart()
        HttpHelper.addEvent(EventType.DIALOG.DIALOG_LANGUAGE_SHOW,eventRemark)
    }

    override fun dismiss() {
        super.dismiss()
        HttpHelper.addEvent(EventType.DIALOG.DIALOG_LANGUAGE_DISMISS,eventRemark)
    }
}